import {createRouter, createWebHistory} from 'vue-router';

// 定义路由
const routes = [
    {
        path: '/',
        redirect: '/login',  // 设置默认 路径  重定向到login页面
    },
    {
        path: '/login',
        component: () => import('@/views/Login.vue')

    },
    {
        path: '/index',
        component: () => import('@/views/index.vue')
    }, {
        path: '/home',
        name: 'Index',
        component: () => import('@/views/home.vue'),  // 首页
        children: [
            {
                path: 'categorylist',
                name: 'categorylist',
                component: () => import('@/views/category/categoryList.vue')
            },
            {
                path: 'addcategory',
                name: 'addcategory',
                component: () => import('@/views/category/addcategory.vue')
            },
            {
                path: 'carousellist',
                name: 'carousellist',
                component: () => import('@/views/carouse/carousellist.vue')
            }
            ,
            {
                path: 'addcarousel',
                name: 'addcarousel',
                component: () => import('@/views/carouse/addcarousel.vue')
            }
            ,
            {
                path: 'addgoods',
                name: 'addgoods',
                component: () => import('@/views/goods/addgoods.vue')
            },
            {
                path: 'goodslist',
                name: 'goodslist',
                component: () => import('@/views/goods/goodslist.vue')
            }
        ]
    },
];


const router = createRouter({
    history: createWebHistory(),  //历史 和哈希模式
    routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {

    // const isLoggedIn = localStorage.getItem('username');
    // if (to.path === '/index') {
    //     return next();
    // }
    // // 如果目标路由是 `/login` 页面，直接放行
    // if (to.path === '/login') {
    //     return next();
    // }
    //
    // // 如果没有登录且目标路由不是登录页面，重定向到登录页面
    // if (isLoggedIn === null) {
    //     return next('/login');
    // }
    //
    // next();
    next()
});
export default router;
